import TreeCell from './TreeCell.vue'

import AccountProduct_product_action_sellable from '@/components/OTree/account/product_product_action_sellable.vue'
import SaleAction_quotations_with_onboarding from '@/components/OTree/sale/action_quotations_with_onboarding.vue'

const module_action_view = {
  account: {
    product_product_action_sellable: 'AccountProduct_product_action_sellable'
  },
  sale: {
    action_quotations_with_onboarding: 'SaleAction_quotations_with_onboarding'
  }
}

// eslint-disable-next-line no-unused-vars
const cp = val => JSON.parse(JSON.stringify(val))

export default {
  name: 'TreeDispatch',
  components: {
    TreeCell,
    AccountProduct_product_action_sellable,
    SaleAction_quotations_with_onboarding
  },
  mixins: [],

  props: {
    viewInfo: {
      type: Object,
      default: () => {
        return {}
      }
    },

    dataInfo: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },

  data() {
    return {}
  },
  computed: {
    record() {
      return this.dataInfo.record || {}
    }
  },
  watch: {},
  async created() {},

  mounted() {},

  render(createElement) {
    const xml_id = this.viewInfo.action.xml_id
    // console.log(xml_id)
    const view_get = () => {
      const [module, xml_id2] = xml_id.split('.')

      const modules = module_action_view[module] || {}
      const view = modules[xml_id2]
      return view
    }

    const view = view_get()

    if (view) {
      return createElement(view, {
        props: { viewInfo: { ...this.viewInfo }, dataInfo: this.dataInfo }
      })
    } else {
      const {
        views: { fields_views }
      } = this.viewInfo

      if (fields_views.list && fields_views.list.type === 'treemob') {
        console.log(cp(fields_views.list))
        return createElement('TreeCell', {
          props: {
            viewInfo: { ...this.viewInfo, view: fields_views.list },
            dataInfo: this.dataInfo
          }
        })
      } else {
        return createElement(
          'van-cell',
          {
            attrs: {
              title: `${this.viewInfo.action.res_model},${this.record.id}`,
              label: xml_id,
              isLink: true
            }
          },
          ['TODO']
        )
      }
    }
  },

  methods: {}
}
